<?php

namespace IDock\Insurance\YAIC\Request;

use IDock\Http\Request;
use IDock\Http\RequestFormat;
use IDock\Http\RequestMethod;
use IDock\Insurance\CHAC\Tools\XMLTools;

class AllYAICRequest extends Request
{

    protected $method = RequestMethod::POST;

    protected $format = RequestFormat::RAW;

    public $transCode = '';

    protected $inLog = true;
    protected $responseLog = true;

    public function loadConfig(array $config): Request
    {
        $this->gateway = $config['gateway'] ?? '';

        $this->logsDir = $config['logsDir'] ?? '';

        $this->timeout = $config['timeout'] ?? '';

        $this->proxy = $config['proxy'] ?? '';

        $this->proxyPW = $config['proxyPW'] ?? '';

        $this->proxyAccount = $config['proxyAccount'] ?? '';

        return $this;
    }

    public function requestLogs($uuid, $inLog = true)
    {
        if (!$this->logsFile) {
            return;
        }

        if ($this->urlTip) {
            $url = $this->gateway;
            $urlLogs = date('Y-m-d H:i:s') . " FUN_TIPS " . $uuid . " " . $this->urlTip . ' ' . $url;
        } else {
            $urlLogs = date('Y-m-d H:i:s') . " URL      " . $uuid . " " . $this->gateway;
        }


        file_put_contents($this->logsFile, $urlLogs . "\r\n", FILE_APPEND);

        //header 日记
        if ($this->headers) {
            $encLogs = date('Y-m-d H:i:s') . " HEADER   " . $uuid . " " . json_encode($this->getHeaders(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            file_put_contents($this->logsFile, $encLogs . "\r\n", FILE_APPEND);
        }

        if (!$inLog) {
            return;
        }

        if ($this->preParameter != $this->bizParam) {
            $preParameter = $this->preParameter;
            if(isset($preParameter['requestBody']['file'])){
                $preParameter['requestBody']['file'] = 'resource...';
            }
            $encLogs = date('Y-m-d H:i:s') . " ENCRYPT  " . $uuid . " " . json_encode($preParameter, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
            file_put_contents($this->logsFile, $encLogs . "\r\n", FILE_APPEND);
        }

        if ($this->bizParam ?? '') {
            $biz = $this->bizParam;
            $xml = simplexml_load_string($biz);
            $res = json_decode(json_encode($xml),true);
            if(isset($res['requestBody']['file'])){
                $res['requestBody']['file'] = 'resource...';
            }
            $reqLogs = date('Y-m-d H:i:s') . " REQUEST  " . $uuid . " " . XMLTools::ToXML($res,'RequestData');
            file_put_contents($this->logsFile, $reqLogs . "\r\n", FILE_APPEND);
        }
    }

    public function responseLogs($uuid, $data)
    {
        if (!$this->logsFile) {
            return;
        }
        $start = strpos($data,'<insurancePolicy>');
        $end = strpos($data,'</insurancePolicy>');
        if($start && $end){
            $search = substr($data,$start,intval($end - $start + strlen('</insurancePolicy>')));
            $data = str_replace($search,"<insurancePolicy>resource...</insurancePolicy>",$data);
        }
        $data = str_replace(["\r\n"],[''],$data);
        $repLogs = date('Y-m-d H:i:s') . " RESPONSE " . $uuid . " " . $data;

        file_put_contents($this->logsFile, $repLogs . "\r\n", FILE_APPEND);
    }

    public function responseAfterLogs($data, $isSuccess)
    {
        parent::responseAfterLogs($data, $isSuccess);
    }

    public function decryptLogs($uuid, $data)
    {
        $res = json_decode($data,true);
        if(isset($res['responseBody']['insurancePolicy'])){
            $res['responseBody']['insurancePolicy'] = 'resource...';
            $data = json_encode($res,320);
        }
        $decLogs = date('Y-m-d H:i:s') . " DECRYPT  " . $uuid . " " . $data;
        $decLogs = str_replace(["\r\n","\r","\n","\t"],['','','',''],$decLogs);
        file_put_contents($this->logsFile, $decLogs . "\r\n", FILE_APPEND);
    }
}
